package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /*
    * 新增菜品和口味
    * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //1.插入菜品
        //对象属性拷贝
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //2.插入口味
        //由于在插入口味的时候菜品id还在没有生产,所以先将添加菜品的时候生产的id返回给dish,
        //在通过dish获取id将id传递给dishFlover中dish_id
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        //由于口味是可选可不选的所以要进行判空
        if(flavors!=null&&flavors.size()>0)
        {
            //插入口味
            //将菜品id添加到每一个口味中的dish_id中
            flavors.forEach(f->{
                f.setDishId(id);
            });
            dishFlavorMapper.insert(flavors);

        }


    }

    //条件分页查询
    @Override
    public PageResult pageRequre(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //强制转换
        Page<DishVO> page=dishMapper.selectPage(dishPageQueryDTO);
        //返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    //批量删除菜品
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //不能删--起售中的菜品
        for (Long id : ids) {
            //根据id查询当前菜品是否起售,如果有一个在起售就抛异常
            Dish dish=dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        }

        //不能删--有关联其他套餐(根据dishId查询是否存在关联的Setmeal_Id)

            //根据dish_id查询套餐setMeal_id
            List<Long> SetMealList =setMealDishMapper.getByDishId(ids);
            if(SetMealList!=null && SetMealList.size()>0)
            {
                //说明有套餐
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }



        //删除菜品表中的数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);
        }



    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查询菜品
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);//拷贝dish属性到vo中
        //根据菜品id查询对应口味
        List<DishFlavor> flavors=dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);//设置vo属性
        return dishVO;
    }

    //根据id修改菜品数据和对应的口味数据
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        //修改口味数据
          //1.清空原有数据
          dishFlavorMapper.deleteByDishId(dishDTO.getId());
          //2.根据dishDTO中的口味数据再重写写入一遍(这里的逻辑跟新增中是一样的)
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0)
        {
            //插入口味
            //将菜品id添加到每一个口味中的dish_id中
            flavors.forEach(f->{
                f.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insert(flavors);

        }

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        //1.根据分类id查询到菜品

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    //根据分类id查询菜品
    @Override
    public List<Dish> getSetmealById(Long categoryId) {
        List<Dish> list=dishMapper.selectDishById(categoryId);
        return list;
    }


    //修改菜品的起售状态
    @Override
    public void setDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDish(dish);
    }


}
